package com.cd.javacxy.module.web.controller;

import com.cd.javacxy.common.model.entity.CxyShuoshuo;
import com.cd.javacxy.common.model.entity.CxyShuoshuoComment;
import com.cd.javacxy.common.model.entity.CxyShuoshuoLike;
import com.cd.javacxy.common.model.entity.SessionUser;
import com.cd.javacxy.common.model.enums.ResponseCode;
import com.cd.javacxy.common.model.query.ShuoShuoQuery;
import com.cd.javacxy.common.model.query.UserFriendQuery;
import com.cd.javacxy.common.model.vo.AjaxResponse;
import com.cd.javacxy.common.model.vo.CxyUserVo;
import com.cd.javacxy.common.utils.Constants;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.base.BaseController;
import com.cd.javacxy.module.web.service.CxyShuoshuoService;
import com.cd.javacxy.module.web.service.CxyUserFriendService;
import com.cd.javacxy.module.web.service.CxyUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * CxyShuoshuo的路由接口服务
 *
 * @author yanghy
 */
@RestController
@RequestMapping("/shuoshuo")
@Slf4j
public class CxyShuoshuoController extends BaseController {

    private CxyShuoshuoService shuoshuoService;

    private CxyUserService cxyUserService;

    private CxyUserFriendService cxyUserFriendService;


    @Autowired
    public void setShuoshuoService(CxyShuoshuoService shuoshuoService) {
        this.shuoshuoService = shuoshuoService;
    }

    @Autowired
    public void setCxyUserService(CxyUserService cxyUserService) {
        this.cxyUserService = cxyUserService;
    }

    @Autowired
    public void setCxyUserFriendService(CxyUserFriendService cxyUserFriendService) {
        this.cxyUserFriendService = cxyUserFriendService;
    }

    @ResponseBody
    @RequestMapping("/list")
    public AjaxResponse<Object> loadShuoShuos(HttpSession session, ShuoShuoQuery shuoShuoQuery) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        try {
            PageResult<CxyShuoshuo> pageResult = this.shuoshuoService.findShuoShuoList(shuoShuoQuery);
            ajaxResponse.setData(pageResult);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
        } catch (Exception e) {
            log.error("加载说说异常", e);
            ajaxResponse.setErrorMsg("加载说说出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }


    @ResponseBody
    @GetMapping("/detail")
    public AjaxResponse<Object> loadShuoShuoDetail(HttpSession session, ShuoShuoQuery shuoShuoQuery) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        try {
            CxyShuoshuo shuoShuo = this.shuoshuoService.findShuoShuo(shuoShuoQuery);
            ajaxResponse.setData(shuoShuo);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
        } catch (Exception e) {
            log.error("加载说说异常", e);
            ajaxResponse.setErrorMsg("加载说说出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }
    @ResponseBody
    @RequestMapping("/publish")
    public AjaxResponse<Object> publicShuoShuo(HttpSession session, CxyShuoshuo shuoShuo){
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if(sessionUser==null){
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        try {
            this.setUserBaseInfo(CxyShuoshuo.class, shuoShuo, session);
            this.shuoshuoService.addShuoShuo(shuoShuo);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
            ajaxResponse.setData(shuoShuo);
        } catch (BussinessException e) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            log.error("{}说说发表出错", shuoShuo.getUserName());
        }
        return ajaxResponse;
    }
    @ResponseBody
    @PostMapping("/comment")
    public AjaxResponse<Object> publicCommentShuoShuo(HttpSession session, CxyShuoshuoComment shuoShuoComment) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionUser == null) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        try {
            this.setUserBaseInfo(CxyShuoshuoComment.class, shuoShuoComment, session);
            this.shuoshuoService.addShuoShuoComment(shuoShuoComment);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
            ajaxResponse.setData(shuoShuoComment);
        } catch (BussinessException e) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            log.error("{}评论出错", shuoShuoComment.getUserName());
        }
        return ajaxResponse;
    }

    @ResponseBody
    @PostMapping("/like")
    public AjaxResponse<Object> doShuoShuoLike(HttpSession session, CxyShuoshuoLike shuoShuoLike) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionUser == null) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        this.setUserBaseInfo(CxyShuoshuoLike.class, shuoShuoLike, session);
        try {
            this.shuoshuoService.doShuoShuoLike(shuoShuoLike);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
            ajaxResponse.setData(shuoShuoLike);
        } catch (BussinessException e) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            log.error("{}点赞出错", shuoShuoLike.getUserName());
        }
        return ajaxResponse;
    }

    @RequestMapping(value = "/{userId}/shuoshuo/{id}")
    public ModelAndView shuoshuo(HttpSession session, @PathVariable Integer userId, @PathVariable Integer id) {
        ModelAndView view = new ModelAndView("user/shuoshuo");
        try {
            CxyUserVo user = this.cxyUserService.findUserInfo4UserHome(userId);
            view.addObject("user", user);
            view.addObject("id", id);
            UserFriendQuery userFriendQuery = new UserFriendQuery();
            userFriendQuery.setUserId(this.getUserId(session));
            userFriendQuery.setFriendUserId(userId);
            view.addObject("focusType", this.cxyUserFriendService.findFocusType4UserHome(userFriendQuery));

            //获取粉丝和关注数量
            userFriendQuery = new UserFriendQuery();
            userFriendQuery.setFriendUserId(userId);
            view.addObject("fansCount", this.cxyUserFriendService.findCount(userFriendQuery));
            userFriendQuery = new UserFriendQuery();
            userFriendQuery.setUserId(userId);
            view.addObject("focusCount", this.cxyUserFriendService.findCount(userFriendQuery));
        } catch (Exception e) {
            log.error("获取说说信息失败：", e);
            view.setViewName("redirect:" + Constants.ERROR_404);
            return view;
        }
        return view;
    }
}
